//
//  ReceiveCellListViewController.m
//  YKMediator_ReceiveModule
//
//  Created by linghit on 2021/11/2.
//

#import "ReceiveCellListViewController.h"
#import "ReceiveViewModel.h"
#import "ReceiveMainCell.h"
#import "UITableView+Empty.h"
#import "YKRefreshHeader.h"
#import "YKRefreshFooter.h"
#import <YKAlert/YKAlertCenter.h>
#import <Masonry/Masonry.h>
#import <YK_ExectionModel/YKExection.h>

@interface ReceiveCellListViewController ()
///
//@property (nonatomic, strong, readwrite) ReceiveViewModel *viewModel;

@end

@implementation ReceiveCellListViewController

- (instancetype)initWithViewModel:(ReceiveViewModel *)viewModel
{
    self = [super init];
    if (self) {
        self.viewModel = viewModel;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
- (void)didSetupUI
{
    [super didSetupUI];
    self.title = @"收款列表";
    self.tableView.backgroundColor = YKHEXCOLOR(@"#F6F7FC");
    self.refreshCommand = ((ReceiveViewModel *)self.viewModel).listCommand;
    [self registerWithClass:ReceiveMainCell.class identifier:NSStringFromClass(ReceiveMainCell.class)];
    self.dataSource = ((ReceiveViewModel *)self.viewModel).lists;
    
}

- (void)didBindData
{
    [super didBindData];
    ReceiveViewModel *vm = ((ReceiveViewModel *)self.viewModel);
    [[vm.saveCommand.executing skip:1] subscribeNext:^(NSNumber * _Nullable x) {
        if ([x boolValue]) {
            [YKAlertCenter showLoading:@"正在保存"];
        }else{
            [YKAlertCenter dissLoading];
        }
    }];
    @weakify(self);
    [vm.saveCommand.executionSignals.switchToLatest subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        [YKAlertCenter showMessage:@"保存成功"];
        [self.header beginRefreshing];
    }];
    [vm.refreshSubject subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        [self.header beginRefreshing];
    }];
    
    [self.header beginRefreshing];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ReceiveViewModel *vm = ((ReceiveViewModel *)self.viewModel);
    @weakify(self);
    @weakify(vm);
    [vm getDetail:indexPath detailBlock:^(NSString * _Nonnull Id, NSMutableAttributedString * _Nonnull detailName, NSMutableAttributedString * _Nonnull price, NSString * _Nonnull createTime, BOOL isCell, NSMutableAttributedString * _Nonnull cellName, NSMutableAttributedString * _Nonnull collcetionName,NSString * _Nonnull title) {
        @strongify(self);
        @strongify(vm);
        if (!isCell) {
            
            UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:[NSString stringWithFormat:@"确认收款:%@",title] preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *action = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                [vm.saveCommand execute:Id];
            }];
            
            UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
            
            [alertVC addAction:action];
            [alertVC addAction:cancel];
            [self presentViewController:alertVC animated:YES completion:nil];
        }else{
            [YKAlertCenter showMessage:@"已收款"];
        }
    }];
}

- (void)dealloc
{
    [self.dataSource removeAllObjects];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
